<template>
  <div class="activity-details">
    <!-- 基本信息区域 -->
    <div class="basic-info">
      <div class="title">{{ details.title }}</div>
      <!-- 添加审核状态和未通过理由的展示 -->
      <div v-if="details.reviewStatus !== '1'" class="review-status-bar">
        <div class="status-info">
          <i :class="details.reviewStatus === '0' ? 'el-icon-loading' : 'el-icon-warning-outline'"></i>
          <span :class="{'status-pending': details.reviewStatus === '0', 'status-reject': details.reviewStatus === '2'}">
            {{ details.reviewStatus === '0' ? '活动审核中' : '活动审核未通过' }}
          </span>
          <!-- 添加修改按钮 -->
          <el-button 
            v-if="details.reviewStatus === '2'"
            type="primary" 
            size="small" 
            @click="showEditDialog"
            class="edit-btn"
          >
            修改信息
          </el-button>
        </div>
        <div v-if="details.reviewStatus === '2' && details.reviewNotes" class="reject-reason">
          未通过原因：{{ details.reviewNotes }}
        </div>
      </div>
      <!-- 其他基本信息保持不变 -->
      <div class="status-tag" v-if="details.briefDescription">{{ details.briefDescription }}</div>
      
      <div class="info-row">
        <i class="el-icon-view"></i>
        <span>{{ details.heatValue || 0 }}</span>
        <i class="el-icon-thumb"></i>
        <span>{{ details.likeCount || 0 }}</span>
      </div>

      <div class="time-location">
        <div class="info-item">
          <i class="el-icon-time"></i>
          <span>{{ formatDate(details.activityTime) }}</span>
        </div>
        <div class="info-item" v-if="details.needRegistration">
          <i class="el-icon-timer"></i>
          <span class="deadline-text">
            报名截止：{{ formatDate(details.registrationDeadline) }}
            <span class="deadline-status" :class="{ 'is-expired': isRegistrationExpired }">
              {{ isRegistrationExpired ? '(已截止)' : '(进行中)' }}
            </span>
          </span>
        </div>
        <div class="info-item">
          <i class="el-icon-location"></i>
          <span>{{ details.location }}</span>
        </div>
        <div class="info-item">
          <i class="el-icon-user"></i>
          <span>{{ details.publisherName }}</span>
        </div>
      </div>
    </div>

    <!-- 活动详情 -->
    <div class="detail-content">
      <div class="section-title">活动详情</div>
      <div class="content-html" v-html="details.details"></div>
    </div>

    <!-- 投票区域 -->
    <div class="vote-section" v-if="details.needVote && details.activityVoteOptions">
      <div class="section-title">投票</div>
      <div class="vote-content">
        <div class="vote-deadline-info">
          <i class="el-icon-timer"></i>
          <span class="deadline-text">
            投票截止：{{ formatDate(details.voteDeadline) }}
            <span class="deadline-status" :class="{ 'is-expired': isVoteExpired }">
              {{ isVoteExpired ? '(已截止)' : '(进行中)' }}
            </span>
          </span>
        </div>

        <div class="vote-theme">{{ details.activityVoteOptions.theme }}</div>
        <div class="vote-options">
          <div class="vote-option" 
               v-for="i in 4" 
               :key="i"
               v-if="details.activityVoteOptions[`option${i}`]"
               :class="{
                 'is-chosen': details.chosenOption === `option${i}`,
                 'can-vote': !details.chosenOption
               }"
               @click="handleVote(`option${i}`)">
            <div class="option-content">
              <span class="option-text">{{ details.activityVoteOptions[`option${i}`] }}</span>
              <span class="vote-count">{{ details.activityVoteOptions[`voteCount${i}`] || 0 }}票</span>
            </div>
            <div class="progress-bar">
              <div class="progress" 
                   :style="{ width: calculateProgress(i) + '%' }"></div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部固定操作栏 -->
    <div class="bottom-actions">
      <div class="action-group">
        <div class="action-item" @click="handleLike">
          <img :src="require(`@/assets/img/${details.likeStatus === '1' ? 'good1' : 'good'}.png`)" 
               class="icon-img" 
               alt="点赞">
          <span>点赞</span>
        </div>
        <div class="action-item" @click="handleCollect">
          <img :src="require(`@/assets/img/${details.collectionStatus === '1' ? 'collection1' : 'collection'}.png`)" 
               class="icon-img" 
               alt="收藏">
          <span>收藏</span>
        </div>
        <div class="action-item" @click="handleForward">
          <i class="el-icon-share"></i>
          <span>转发 {{ details.forwardCount || 0 }}</span>
        </div>
        <div class="action-item" @click="handleContact">
          <img src="@/assets/img/contact.png" class="icon-img" alt="联系">
          <span>联系</span>
        </div>
      </div>
      <div class="main-action">
        <template v-if="details.needRegistration">
          <div class="budget-info" v-if="details.budget">
            <span class="amount">¥{{ details.budget }}</span>
            <span class="per-person">/人</span>
          </div>
          <template v-if="details.registrationStatus === '1'">
            <el-button 
              type="danger" 
              size="small"
              plain
              @click="handleCancelSignUp"
            >
              取消报名
            </el-button>
          </template>
          <el-button 
            v-else
            type="primary" 
            :disabled="isRegistrationExpired || !isActivityReviewPassed"
            @click="handleRegister"
          >
            {{ getRegistrationButtonText }}
          </el-button>
        </template>
        <el-button v-else type="info" disabled>无需报名</el-button>
      </div>
    </div>

    <!-- 联系方式弹窗 -->
    <el-dialog
      title="联系方式"
      :visible.sync="contactDialogVisible"
      width="80%"
      custom-class="contact-dialog"
      :show-close="false"
      center>
      <div class="contact-content">
        <div class="contact-item">
          <span class="label">联系人</span>
          <span class="value">{{ details.publisherName }}</span>
        </div>
        <div class="contact-item">
          <span class="label">{{ details.contactType }}</span>
          <span class="value">{{ details.contactInfo }}</span>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="contactDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 报名人列表，仅在需要报名时显示 -->
    <div class="signup-list section" v-if="details.needRegistration">
      <div class="section-title">
        <span>报名人列表</span>
        <el-tag size="small" type="primary">{{ details.registrationCount || 0 }}人</el-tag>
      </div>
      
      <!-- 仅在我创建的活动中显示详细列表 -->
      <div class="list-content" v-if="isMyCreated">
        <template v-if="signUpList.length > 0">
          <el-table
            :data="signUpList"
            style="width: 100%"
            :header-cell-style="{ background: '#f5f7fa' }"
            border
            @row-click="handleRowClick"
          >
            <el-table-column
              prop="signUpName"
              label="姓名"
              min-width="100"
              align="center"
            >
              <template slot-scope="scope">
                <div class="name-cell">
                  <span>{{ scope.row.signUpName }}</span>
                </div>
              </template>
            </el-table-column>
            
            <el-table-column
              prop="signUpContact"
              label="联系方式"
              min-width="140"
              align="center"
            >
              <template slot-scope="scope">
                <div class="contact-cell">
                  <i class="el-icon-phone"></i>
                  <el-tooltip 
                    :content="scope.row.signUpContact" 
                    placement="top"
                    :disabled="!scope.row.signUpContact"
                  >
                    <span>{{ scope.row.signUpContact || '暂无' }}</span>
                  </el-tooltip>
                </div>
              </template>
            </el-table-column>
            
            <el-table-column
              prop="signUpTime"
              label="报名时间"
              min-width="120"
              align="center"
            >
              <template slot-scope="scope">
                <div class="time-cell">
                  <i class="el-icon-time"></i>
                  <span>{{ formatSignUpTime(scope.row.signUpTime) }}</span>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <el-empty 
          v-else 
          description="暂无报名" 
          :image-size="60"
        >
          <template slot="description">
            <p>暂无校友报名</p>
            <small style="color: #999; font-size: 12px;">分享给更多校友，让大家一起参与</small>
          </template>
        </el-empty>
      </div>
    </div>

    <!-- 校友详情弹窗 -->
    <el-dialog
      title="校友详情"
      :visible.sync="alumniDialogVisible"
      :width="isMobile ? '96%' : '90%'"
      custom-class="alumni-dialog"
      :close-on-click-modal="false"
    >
      <el-card v-if="alumniDetail" shadow="never" class="alumni-card">
        <!-- 基本信息 -->
        <div class="info-section">
          <div class="section-header">
            <i class="el-icon-user"></i>
            <span>基本信息</span>
          </div>
          <el-descriptions :column="isMobile ? 1 : 2" border>
            <el-descriptions-item label="姓名">
              {{ alumniDetail.name }}
            </el-descriptions-item>
            <el-descriptions-item label="性别">
              {{ alumniDetail.gender }}
            </el-descriptions-item>
            <el-descriptions-item label="入学年份">
              {{ alumniDetail.admissionYear }}
            </el-descriptions-item>
            <el-descriptions-item label="毕业年份">
              {{ alumniDetail.graduationYear }}
            </el-descriptions-item>
            <el-descriptions-item label="学院">
              {{ alumniDetail.college }}
            </el-descriptions-item>
            <el-descriptions-item label="专业">
              {{ alumniDetail.major }}
            </el-descriptions-item>
            <el-descriptions-item label="班级">
              {{ alumniDetail.classNo }}
            </el-descriptions-item>
          </el-descriptions>
        </div>
        
        <!-- 联系方式 -->
        <div class="info-section">
          <div class="section-header">
            <i class="el-icon-phone"></i>
            <span>联系方式</span>
          </div>
          <el-descriptions :column="isMobile ? 1 : 2" border>
            <el-descriptions-item label="联系电话">
              <el-tag size="medium" type="info">
                {{ alumniDetail.contactInfo || '暂无' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="邮箱">
              <el-tag size="medium" type="info">
                {{ alumniDetail.email || '暂无' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="现居地">
              {{ alumniDetail.currentResidence || '暂无' }}
            </el-descriptions-item>
            <el-descriptions-item label="家乡">
              {{ alumniDetail.hometown || '暂无' }}
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </el-card>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="alumniDialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加编辑弹窗 -->
    <el-dialog
      title="修改活动信息"
      :visible.sync="editDialogVisible"
      width="90%"
      :close-on-click-modal="false"
      class="edit-dialog"
    >
      <el-form 
        ref="editForm" 
        :model="editForm" 
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item label="活动标题" prop="title">
          <el-input v-model="editForm.title" placeholder="请输入活动标题"></el-input>
        </el-form-item>
        
        <el-form-item label="活动时间" prop="activityTime">
          <el-date-picker
            v-model="editForm.activityTime"
            type="datetime"
            placeholder="选择活动时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        
        <el-form-item label="活动地点" prop="location">
          <el-input v-model="editForm.location" placeholder="请输入活动地点"></el-input>
        </el-form-item>
        
        <el-form-item label="活动简介" prop="briefDescription">
          <el-input v-model="editForm.briefDescription" placeholder="请输入活动简介"></el-input>
        </el-form-item>
        
        <el-form-item label="活动详情" prop="details">
          <el-input 
            type="textarea" 
            v-model="editForm.details" 
            :rows="4"
            placeholder="请输入活动详情"
          ></el-input>
        </el-form-item>
        
        <el-form-item label="人均预算">
          <el-input v-model="editForm.budget" placeholder="请输入人均预算（选填）"></el-input>
        </el-form-item>
        
        <el-form-item label="联系方式" prop="contactType">
          <el-input v-model="editForm.contactType" placeholder="请输入联系方式类型"></el-input>
        </el-form-item>
        
        <el-form-item label="联系信息" prop="contactInfo">
          <el-input v-model="editForm.contactInfo" placeholder="请输入联系信息"></el-input>
        </el-form-item>
        
        <el-form-item label="需要报名">
          <el-switch v-model="editForm.needRegistration"></el-switch>
        </el-form-item>
        
        <el-form-item label="报名截止" v-if="editForm.needRegistration" prop="registrationDeadline">
          <el-date-picker
            v-model="editForm.registrationDeadline"
            type="datetime"
            placeholder="选择报名截止时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        
        <el-form-item label="需要投票">
          <el-switch v-model="editForm.needVote"></el-switch>
        </el-form-item>
        
        <template v-if="editForm.needVote">
          <el-form-item label="投票主题" prop="activityVoteOptions.theme">
            <el-input v-model="editForm.activityVoteOptions.theme" placeholder="请输入投票主题"></el-input>
          </el-form-item>
          
          <el-form-item 
            v-for="i in 4" 
            :key="i"
            :label="`选项${i}`"
            :prop="`activityVoteOptions.option${i}`"
          >
            <el-input 
              v-model="editForm.activityVoteOptions[`option${i}`]" 
              :placeholder="`请输入选项${i}`"
            ></el-input>
          </el-form-item>
          
          <el-form-item label="投票截止" prop="voteDeadline">
            <el-date-picker
              v-model="editForm.voteDeadline"
              type="datetime"
              placeholder="选择投票截止时间"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </el-form-item>
        </template>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdate" :loading="updating">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加取消报名确认弹框 -->
    <el-dialog
      title="取消报名确认"
      :visible.sync="cancelSignUpVisible"
      width="90%"
      custom-class="cancel-signup-dialog"
      :show-close="false"
      center
    >
      <div class="cancel-content">
        <i class="el-icon-warning-outline warning-icon"></i>
        <div class="warning-text">
          <p class="title">确定要取消报名吗？</p>
          <p class="desc">取消后需要重新报名才能参加活动</p>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelSignUpVisible = false">再想想</el-button>
        <el-button type="danger" @click="confirmCancelSignUp" :loading="cancelling">确定取消</el-button>
      </span>
    </el-dialog>

    <!-- 审批操作区域 -->
    <div v-if="isAudit" class="audit-actions">
      <div class="audit-form">
        <el-form :model="auditForm" ref="auditForm" :rules="auditRules" size="small">
          <el-form-item 
            prop="reasonForReview" 
            v-if="auditForm.examine === '2'"
          >
            <el-input
              type="textarea"
              v-model="auditForm.reasonForReview"
              placeholder="请输入不通过原因（必填）"
              :rows="3"
              resize="none"
              maxlength="200"
              show-word-limit
            ></el-input>
          </el-form-item>
          <div class="audit-buttons">
            <el-button 
              type="success" 
              plain
              icon="el-icon-check"
              @click="handleAudit('1')"
              :loading="auditing"
            >通过</el-button>
            <el-button 
              type="danger" 
              plain
              icon="el-icon-close"
              @click="handleAudit('2')"
              :loading="auditing"
            >不通过</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { 
  getAlumniActivityFormDetail, 
  activityHandleLike, 
  activityHandleCollection, 
  activityHandleForward,
  activityHandleVote,
  activityHandleSignup,  // 导入报名接口
  getMyActivitySignUp,
  getAlumniDetail,
  updateActivity,
  alumniActivityFormCancelSignUp,
  checkActivity
} from "@/api/api";

export default {
  data() {
    return {
      details: {},
      contactDialogVisible: false,  // 添加控制弹窗显示的变量
      signUpList: [],  // 报名列表数据
      isMyCreated: false,  // 是否是我创建的活动
      isMobile: document.body.clientWidth < 768,
      alumniDialogVisible: false,
      alumniDetail: null,
      editDialogVisible: false,
      updating: false,
      editForm: {
        id: '',
        title: '',
        activityTime: '',
        location: '',
        briefDescription: '',
        details: '',
        budget: '',
        contactType: '',
        contactInfo: '',
        needRegistration: false,
        registrationDeadline: '',
        needVote: false,
        voteDeadline: '',
        activityVoteOptions: {
          theme: '',
          option1: '',
          option2: '',
          option3: '',
          option4: ''
        }
      },
      formRules: {
        title: [{ required: true, message: '请输入活动标题', trigger: 'blur' }],
        activityTime: [{ required: true, message: '请选择活动时间', trigger: 'change' }],
        location: [{ required: true, message: '请输入活动地点', trigger: 'blur' }],
        briefDescription: [{ required: true, message: '请输入活动简介', trigger: 'blur' }],
        details: [{ required: true, message: '请输入活动详情', trigger: 'blur' }],
        contactType: [{ required: true, message: '请输入联系方式类型', trigger: 'blur' }],
        contactInfo: [{ required: true, message: '请输入联系信息', trigger: 'blur' }]
      },
      cancelSignUpVisible: false,
      cancelling: false,
      forwarding: false,
      isAudit: false,
      auditing: false,
      auditForm: {
        examine: '',
        reasonForReview: ''
      },
      auditRules: {
        reasonForReview: [
          { required: true, message: '请输入不通过原因', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    document.title = "活动详情";
    this.getActivityDetail();
    this.isAudit = this.$route.query.isAudit==true;
  },
  mounted() {
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    async getActivityDetail() {
      try {
        const res = await getAlumniActivityFormDetail({
          id: this.$route.query.id
        });
        
        if (res.success) {
          this.details = res.data;
          this.isMyCreated = this.details.isCreator === '1';
          
          // 如果是我创建的活动且需要报名，获取报名列表
          if (this.isMyCreated && this.details.needRegistration) {
            this.getSignUpList();
          }
        } else {
          this.$message.error('获取详情失败');
        }
      } catch (error) {
        console.error('获取详情失败:', error);
        this.$message.error('获取详情失败，请稍后重试');
      }
    },
    
    // 点赞
    async handleLike() {
      try {
        const params = {
          id: this.details.id
        }
        const res = await activityHandleLike(params)
        
        if (res.success) {
          // 更新点赞状态
          this.details.likeStatus = this.details.likeStatus === '1' ? '0' : '1'
          // 更新点赞数量
          this.details.likeCount = this.details.likeStatus === '1' 
            ? Number(this.details.likeCount) + 1 
            : Number(this.details.likeCount) - 1
            
          this.$message({
            type: 'success',
            message: this.details.likeStatus === '1' ? '点赞成功' : '取消点赞成功'
          })
        }
      } catch (error) {
        console.error('点赞操作失败:', error)
        this.$message.error('操作失败，请稍后重试')
      }
    },
    
    // 收藏
    async handleCollect() {
      try {
        const params = {
          id: this.details.id
        }
        const res = await activityHandleCollection(params)
        
        if (res.success) {
          // 更新收藏状态
          this.details.collectionStatus = this.details.collectionStatus === '1' ? '0' : '1'
          // 更新收藏数量
          this.details.collectionCount = this.details.collectionStatus === '1' 
            ? Number(this.details.collectionCount) + 1 
            : Number(this.details.collectionCount) - 1
            
          this.$message({
            type: 'success',
            message: this.details.collectionStatus === '1' ? '收藏成功' : '取消收藏成功'
          })
        }
      } catch (error) {
        console.error('收藏操作失败:', error)
        this.$message.error('操作失败，请稍后重试')
      }
    },
    
    // 转发
    async handleForward() {
      if (this.forwarding) return;
      
      try {
        this.forwarding = true;
        const res = await activityHandleForward({
          id: this.details.id
        });
        
        if (res.code === 200) {
          // 创建临时输入框
          const input = document.createElement('input');
          input.value = res.data; // 接口返回的链接
          document.body.appendChild(input);
          
          // 选中并复制
          input.select();
          document.execCommand('copy');
          
          // 移除临时输入框
          document.body.removeChild(input);
          
          this.$message({
            type: 'success',
            message: '转发链接已复制到剪贴板！',
            duration: 2000
          });
          
          // 更新转发数
          this.details.forwardCount = (this.details.forwardCount || 0) + 1;
        } else {
          this.$message.error(res.message || '转发失败');
        }
      } catch (error) {
        console.error('转发失败:', error);
        this.$message.error('转发失败，请稍后重试');
      } finally {
        this.forwarding = false;
      }
    },
    
    // 联系
    handleContact() {
      if (!this.isActivityReviewPassed) {
        this.$message.warning(
          this.details.reviewStatus === '0' 
            ? '活动正在审核中，暂时无法查看联系方式' 
            : '活动审核未通过，无法查看联系方式'
        );
        return;
      }
      
      if (this.details.contactInfo) {
        this.contactDialogVisible = true;
      } else {
        this.$message.info('暂无联系方式');
      }
    },
    
    // 报名
    async handleRegister() {
      // 检查审核状态
      if (!this.isActivityReviewPassed) {
        this.$message.warning(
          this.details.reviewStatus === '0' 
            ? '活动正在审核中，暂时无法报名' 
            : '活动审核未通过，无法报名'
        );
        return;
      }

      // 如果已经报名，直接返回
      if (this.details.registrationStatus === '1') {
        return;
      }
      
      // 检查是否在报名截止时间之前
      if (this.isRegistrationExpired) {
        this.$message.warning('报名已截止');
        return;
      }

      try {
        const params = {
          id: this.details.id
        }
        const res = await activityHandleSignup(params)
        
        if (res.success) {
          this.details.registrationStatus = '1'
          this.details.registrationCount = Number(this.details.registrationCount || 0) + 1
          this.$message.success('报名成功');
          this.getActivityDetail();
        }
      } catch (error) {
        console.error('报名失败:', error);
        this.$message.error('报名失败，请稍后重试');
      }
    },

    formatDate(date) {
      if (!date) return '';
      let formatDate = new Date(date);
      
      const o = {
        'M+': formatDate.getMonth() + 1,
        'd+': formatDate.getDate(),
        'H+': formatDate.getHours(),
        'm+': formatDate.getMinutes(),
        's+': formatDate.getSeconds(),
        'q+': Math.floor((formatDate.getMonth() + 3) / 3),
        'S': formatDate.getMilliseconds()
      }
      
      let format = 'yyyy-MM-dd HH:mm';
      if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (formatDate.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      
      for (let k in o) {
        if (new RegExp('(' + k + ')').test(format)) {
          format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
        }
      }
      
      return format
    },

    // 处理投票
    async handleVote(option) {
      // 检查是否已过期
      if (this.isVoteExpired) {
        this.$message.warning('投票已截止');
        return;
      }

      // 如果已投票且选择相同选项，不做处理
      if (this.details.chosenOption === option) {
        return;
      }

      // 如果已投票且选择不同选项，弹出确认框
      if (this.details.chosenOption) {
        try {
          await this.$confirm(
            '您已经投过票了，是否重新投票？', 
            '提示', 
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }
          );
        } catch (err) {
          return; // 用户取消重新投票
        }
      }
      
      // 执行投票
      try {
        const params = {
          activityId: this.details.id,
          voteId: option
        }
        const res = await activityHandleVote(params)
        
        if (res.success) {
          // 如果是重新投票，先减去原来选项的票数
          if (this.details.chosenOption) {
            const oldVoteCountKey = `voteCount${this.details.chosenOption.slice(-1)}`;
            this.details.activityVoteOptions[oldVoteCountKey] = 
              Math.max(0, Number(this.details.activityVoteOptions[oldVoteCountKey] || 0) - 1);
          }

          // 更新新选项的票数
          const newVoteCountKey = `voteCount${option.slice(-1)}`;
          this.details.activityVoteOptions[newVoteCountKey] = 
            Number(this.details.activityVoteOptions[newVoteCountKey] || 0) + 1;

          // 更新选中状态
          this.details.chosenOption = option;
            
          this.$message({
            type: 'success',
            message: this.details.chosenOption ? '重新投票成功' : '投票成功'
          });
        }
      } catch (error) {
        console.error('投票失败:', error);
        this.$message.error('投票失败，请稍后重试');
      }
    },

    // 计算投票进度条
    calculateProgress(optionIndex) {
      const options = this.details.activityVoteOptions;
      let totalVotes = 0;
      let currentVotes = Number(options[`voteCount${optionIndex}`] || 0);
      
      // 计算总票数
      for (let i = 1; i <= 4; i++) {
        if (options[`option${i}`]) {
          totalVotes += Number(options[`voteCount${i}`] || 0);
        }
      }
      
      if (totalVotes === 0) return 0;
      return (currentVotes / totalVotes) * 100;
    },

    // 获取报名列表
    async getSignUpList() {
      try {
        const res = await getMyActivitySignUp({
          id: this.$route.query.id
        });
        if (res.code === 200) {
          this.signUpList = res.data || [];
        }
      } catch (error) {
        console.error('获取报名列表失败:', error);
        this.$message.error('获取报名列表失败');
      }
    },
    
    // 处理行点击事件，查看校友详情
    async handleRowClick(row) {
      try {
        const res = await getAlumniDetail({
          id: row.signUpId
        });
        if (res.code === 200) {
          this.alumniDetail = res.data;
          this.alumniDialogVisible = true;
        }
      } catch (error) {
        console.error('获取校友详情失败:', error);
        this.$message.error('获取校友详情失败');
      }
    },
    
    // 格式化报名时间
    formatSignUpTime(time) {
      if (!time) return '';
      const date = new Date(time);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    
    handleResize() {
      this.isMobile = document.body.clientWidth < 768;
    },

    showEditDialog() {
      // 复制当前活动信息到编辑表单
      this.editForm = {
        ...this.details,
        activityVoteOptions: {
          ...this.details.activityVoteOptions
        }
      };
      this.editDialogVisible = true;
    },
    
    async handleUpdate() {
      try {
        await this.$refs.editForm.validate();
        
        this.updating = true;
        const res = await updateActivity(this.editForm);
        
        if (res.code === 200) {
          this.$message.success('修改成功');
          this.editDialogVisible = false;
          // 刷新页面数据
          this.getActivityDetail();
        } else {
          this.$message.error(res.message || '修改失败');
        }
      } catch (error) {
        console.error('修改活动失败:', error);
        this.$message.error('修改失败，请稍后重试');
      } finally {
        this.updating = false;
      }
    },

    handleCancelSignUp() {
      this.cancelSignUpVisible = true;
    },
    
    async confirmCancelSignUp() {
      try {
        this.cancelling = true;
        const res = await alumniActivityFormCancelSignUp({
          id: this.details.id
        });
        
        if (res.code === 200) {
          this.$message.success('取消报名成功');
          this.cancelSignUpVisible = false;
          // 刷新活动详情
          this.getActivityDetail();
        } else {
          this.$message.error(res.message || '取消报名失败');
        }
      } catch (error) {
        console.error('取消报名失败:', error);
        this.$message.error('取消报名失败，请稍后重试');
      } finally {
        this.cancelling = false;
      }
    },

    async handleAudit(examine) {
      this.auditForm.examine = examine;
      
      if (examine === '2' && !this.auditForm.reasonForReview) {
        this.$refs.auditForm.validate(async (valid) => {
          if (!valid) return;
        });
        return;
      }
      
      try {
        this.auditing = true;
        const res = await checkActivity({
          id: this.details.id,
          examine: examine,
          reasonForReview: this.auditForm.reasonForReview
        });
        
        if (res.code === 200) {
          this.$message.success('审批成功');
          // 返回上一页
          this.$router.go(-1);
        } else {
          this.$message.error(res.message || '审批失败');
        }
      } catch (error) {
        console.error('审批失败:', error);
        this.$message.error('审批失败，请稍后重试');
      } finally {
        this.auditing = false;
      }
    }
  },
  computed: {
    isRegistrationExpired() {
      if (!this.details.registrationDeadline) return false;
      return new Date() > new Date(this.details.registrationDeadline);
    },
    isVoteExpired() {
      if (!this.details.voteDeadline) return false;
      return new Date() > new Date(this.details.voteDeadline);
    },
    isActivityReviewPassed() {
      return this.details.reviewStatus === '1';
    },
    getRegistrationButtonText() {
      if (!this.isActivityReviewPassed) {
        return this.details.reviewStatus === '0' ? '活动审核中' : '活动审核未通过';
      }
      if (this.details.registrationStatus === '1') {
        return '已报名';
      }
      if (this.isRegistrationExpired) {
        return '报名已截止';
      }
      return '立即报名';
    }
  }
};
</script>

<style lang="less" scoped>
.activity-details {
  min-height: 100vh;
  background: #f5f7fa;
  padding-bottom: 60px;

  .basic-info {
    background: #fff;
    padding: 15px;
    margin-bottom: 10px;

    .title {
      font-size: 18px;
      font-weight: bold;
      color: #333;
      margin-bottom: 10px;
    }

    .status-tag {
      display: inline-block;
      padding: 2px 8px;
      background: #e6f3ff;
      color: #4790ff;
      border-radius: 4px;
      font-size: 12px;
      margin-bottom: 10px;
    }

    .info-row {
      display: flex;
      align-items: center;
      gap: 8px;
      color: #999;
      font-size: 13px;
      margin-bottom: 10px;

      i {
        font-size: 14px;
      }
    }

    .time-location {
      .info-item {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 8px;
        color: #666;
        font-size: 14px;

        i {
          color: #999;
        }

        .deadline-text {
          display: flex;
          align-items: center;
          gap: 8px;

          .deadline-status {
            font-size: 12px;
            padding: 0 4px;
            border-radius: 2px;
            background: #67C23A;
            color: #fff;

            &.is-expired {
              background: #F56C6C;
            }
          }
        }
      }
    }

    .review-status-bar {
      margin: 12px 0;
      padding: 12px;
      background-color: #fef0f0;
      border-radius: 4px;
      border: 1px solid #fde2e2;

      .status-info {
        display: flex;
        align-items: center;
        gap: 8px;
        
        i {
          font-size: 16px;
          
          &.el-icon-loading {
            color: #e6a23c;
          }
          
          &.el-icon-warning-outline {
            color: #f56c6c;
          }
        }

        .status-pending {
          color: #e6a23c;
          font-weight: 500;
        }

        .status-reject {
          color: #f56c6c;
          font-weight: 500;
        }
      }

      .reject-reason {
        margin-top: 8px;
        padding-top: 8px;
        border-top: 1px solid #fde2e2;
        color: #f56c6c;
        font-size: 14px;
        line-height: 1.4;
      }

      .edit-btn {
        margin-left: 12px;
      }
    }
  }

  .detail-content {
    background: #fff;
    padding: 15px;

    .section-title {
      font-size: 16px;
      font-weight: 500;
      color: #333;
      margin-bottom: 10px;
    }

    .content-html {
      color: #666;
      line-height: 1.6;
      font-size: 14px;
    }
  }

  .vote-section {
    background: #fff;
    padding: 15px;
    margin-bottom: 10px;

    .vote-content {
      .vote-deadline-info {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 12px;
        color: #666;
        font-size: 14px;

        i {
          color: #999;
        }

        .deadline-text {
          display: flex;
          align-items: center;
          gap: 8px;

          .deadline-status {
            font-size: 12px;
            padding: 0 4px;
            border-radius: 2px;
            background: #67C23A;
            color: #fff;

            &.is-expired {
              background: #F56C6C;
            }
          }
        }
      }

      .vote-theme {
        font-size: 15px;
        font-weight: 500;
        color: #333;
        margin-bottom: 12px;
      }

      .vote-options {
        .vote-option {
          margin-bottom: 12px;
          padding: 12px;
          background: #f5f7fa;
          border-radius: 8px;
          position: relative;
          overflow: hidden;
          
          &.can-vote {
            cursor: pointer;
            
            &:hover {
              background: #e8f3ff;
            }
          }
          
          &.is-chosen {
            background: #e8f3ff;
            border: 1px solid #4790ff;
            
            .option-content {
              color: #4790ff;
            }
          }

          &:not(.is-chosen) {
            cursor: pointer;
            
            &:hover {
              background: #e8f3ff;
            }
          }

          .option-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: relative;
            z-index: 1;

            .option-text {
              font-size: 14px;
              color: #333;
            }

            .vote-count {
              font-size: 13px;
              color: #666;
            }
          }

          .progress-bar {
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            width: 100%;
            background: transparent;

            .progress {
              height: 100%;
              background: rgba(71, 144, 255, 0.1);
              transition: width 0.3s ease;
            }
          }
        }
      }
    }
  }

  .bottom-actions {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #fff;
    padding: 8px 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.05);

    .action-group {
      display: flex;
      gap: 20px;

      .action-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 4px;

        .icon-img {
          width: 20px;
          height: 20px;
        }

        span {
          font-size: 12px;
          color: #666;
        }
      }
    }

    .main-action {
      display: flex;
      align-items: center;
      gap: 10px;  // 添加按钮之间的间距
      
      .el-button--danger.is-plain {
        margin-right: 10px;  // 取消报名按钮和报名按钮之间的间距
      }

      .budget-info {
        text-align: right;
        margin-right: 8px;

        .amount {
          font-size: 18px;
          font-weight: 500;
          color: #f56c6c;
        }

        .per-person {
          font-size: 12px;
          color: #999;
        }
      }

      .el-button {
        padding: 8px 24px;

        &.is-disabled {
          &.el-button--primary {
            background-color: #f5f7fa;
            border-color: #e4e7ed;
            color: #909399;
          }
        }
      }
    }
  }

  // 弹窗样式
  :deep(.contact-dialog) {
    border-radius: 8px;
    
    .el-dialog__header {
      padding: 15px;
      text-align: center;
      border-bottom: 1px solid #eee;
      
      .el-dialog__title {
        font-size: 16px;
        font-weight: 500;
        color: #333;
      }
    }
    
    .el-dialog__body {
      padding: 20px;
      
      .contact-content {
        .contact-item {
          display: flex;
          align-items: center;
          padding: 10px 0;
          
          &:not(:last-child) {
            border-bottom: 1px solid #f5f5f5;
          }
          
          .label {
            width: 80px;
            color: #999;
            font-size: 14px;
          }
          
          .value {
            flex: 1;
            color: #333;
            font-size: 14px;
            font-weight: 500;
          }
        }
      }
    }
    
    .el-dialog__footer {
      padding: 10px 20px 15px;
      text-align: center;
      border-top: 1px solid #eee;
      
      .el-button {
        width: 120px;
      }
    }
  }

  // 报名人列表样式
  .signup-list {
    background: #fff;
    padding: 15px;
    margin: 10px 0 60px;  // 底部留出空间给底部操作栏

    .section-title {
      display: flex;
      align-items: center;
      gap: 10px;
      font-size: 16px;
      font-weight: 500;
      color: #333;
      margin-bottom: 15px;
      
      .el-tag {
        margin-top: 1px;
      }
    }

    .list-content {
      .name-cell {
        font-size: 14px;
        font-weight: 500;
        color: #333;
      }

      .contact-cell {
        font-size: 14px;
        color: #666;
      }

      .time-cell {
        font-size: 14px;
        color: #999;
      }
    }
  }

  :deep(.alumni-dialog) {
    .el-dialog__header {
      padding: 15px 20px;
      background: #f5f7fa;
      border-bottom: 1px solid #eee;
      margin: 0;
      
      .el-dialog__title {
        font-size: 16px;
        font-weight: 500;
        color: #333;
      }
    }
    
    .el-dialog__body {
      padding: 20px;
    }
    
    .alumni-card {
      border: none;
      
      .info-section {
        margin-bottom: 24px;
        
        &:last-child {
          margin-bottom: 0;
        }
        
        .section-header {
          display: flex;
          align-items: center;
          margin-bottom: 16px;
          
          i {
            font-size: 18px;
            color: #409eff;
            margin-right: 8px;
          }
          
          span {
            font-size: 16px;
            font-weight: 500;
            color: #333;
          }
        }
      }
      
      :deep(.el-descriptions) {
        .el-descriptions-item__label {
          width: 100px;
          background: #fafafa;
          color: #666;
        }
        
        .el-descriptions-item__content {
          color: #333;
          
          .el-tag {
            background: #f5f7fa;
            border-color: #e4e7ed;
            color: #333;
          }
        }
      }
    }
    
    .dialog-footer {
      text-align: right;
      padding-top: 10px;
      border-top: 1px solid #eee;
    }

    // 移动端适配
    @media screen and (max-width: 768px) {
      .el-dialog {
        margin-top: 10vh !important;
      }
      
      .el-dialog__header {
        padding: 12px 15px;
        
        .el-dialog__title {
          font-size: 15px;
        }
      }
      
      .el-dialog__body {
        padding: 15px;
      }
      
      .alumni-card {
        .info-section {
          margin-bottom: 20px;
          
          .section-header {
            margin-bottom: 12px;
            
            i {
              font-size: 16px;
            }
            
            span {
              font-size: 15px;
            }
          }
        }
        
        :deep(.el-descriptions) {
          .el-descriptions-item__label {
            width: 80px;
            padding: 8px 10px;
            font-size: 13px;
          }
          
          .el-descriptions-item__content {
            padding: 8px 10px;
            font-size: 13px;
            
            .el-tag {
              font-size: 12px;
              height: 24px;
              line-height: 22px;
            }
          }
        }
      }
      
      .dialog-footer {
        padding: 10px 15px;
        
        .el-button {
          padding: 8px 15px;
          font-size: 13px;
        }
      }
    }
  }

  .edit-dialog {
    /deep/ .el-dialog__body {
      max-height: 70vh;
      overflow-y: auto;
      padding: 20px;
    }
    
    .el-form-item {
      margin-bottom: 20px;
    }
    
    .el-date-picker {
      width: 100%;
    }
  }

  .cancel-signup-dialog {
    /deep/ .el-dialog__header {
      padding: 20px 20px 10px;
      text-align: center;
      
      .el-dialog__title {
        font-size: 18px;
        font-weight: 500;
        color: #333;
      }
    }
    
    /deep/ .el-dialog__body {
      padding: 20px 20px 30px;
    }
    
    .cancel-content {
      display: flex;
      flex-direction: column;
      align-items: center;
      text-align: center;
      
      .warning-icon {
        font-size: 48px;
        color: #E6A23C;
        margin-bottom: 20px;
      }
      
      .warning-text {
        .title {
          font-size: 16px;
          font-weight: 500;
          color: #333;
          margin-bottom: 8px;
        }
        
        .desc {
          font-size: 14px;
          color: #909399;
        }
      }
    }
    
    /deep/ .el-dialog__footer {
      padding: 10px 20px 20px;
      text-align: center;
      
      .dialog-footer {
        .el-button {
          min-width: 100px;
          margin: 0 10px;
        }
      }
    }
  }

  // 移动端适配
  @media screen and (max-width: 768px) {
    .activity-details {
      .basic-info {
        .review-status-bar {
          margin: 10px 0;
          padding: 10px;

          .reject-reason {
            margin-top: 6px;
            padding-top: 6px;
          }
        }
      }
    }

    .cancel-signup-dialog {
      /deep/ .el-dialog {
        width: 90% !important;
      }
      
      /deep/ .el-dialog__header {
        padding: 15px 15px 10px;
        
        .el-dialog__title {
          font-size: 16px;
        }
      }
      
      /deep/ .el-dialog__body {
        padding: 15px 15px 25px;
      }
      
      .cancel-content {
        .warning-icon {
          font-size: 40px;
          margin-bottom: 15px;
        }
        
        .warning-text {
          .title {
            font-size: 15px;
          }
          
          .desc {
            font-size: 13px;
          }
        }
      }
      
      /deep/ .el-dialog__footer {
        padding: 10px 15px 15px;
      }
    }
  }

  .audit-actions {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: #fff;
    padding: 15px 20px;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
    z-index: 2001;  // 确保在导航栏之上
    
    .audit-form {
      max-width: 500px;
      margin: 0 auto;
      
      .el-form-item {
        margin-bottom: 15px;
        
        /deep/ .el-textarea__inner {
          padding: 10px 15px;
          font-size: 14px;
          border-radius: 4px;
          background-color: #f8f9fb;
          border: 1px solid #e4e7ed;
          
          &:focus {
            background-color: #fff;
            border-color: #409EFF;
          }
          
          &::placeholder {
            color: #909399;
          }
        }
        
        /deep/ .el-textarea__count {
          color: #909399;
          font-size: 12px;
          background: transparent;
          padding: 0 5px;
        }
      }
      
      .audit-buttons {
        display: flex;
        justify-content: center;
        gap: 20px;
        margin-top: 15px;
        
        .el-button {
          min-width: 100px;
          padding: 10px 20px;
          font-size: 14px;
          border-radius: 4px;
          
          &.el-button--success {
            &:hover, &:focus {
              color: #fff;
              background-color: #67c23a;
              border-color: #67c23a;
            }
          }
          
          &.el-button--danger {
            &:hover, &:focus {
              color: #fff;
              background-color: #f56c6c;
              border-color: #f56c6c;
            }
          }
          
          i {
            margin-right: 5px;
          }
        }
      }
    }
  }

  @media screen and (max-width: 768px) {
    .audit-actions {
      padding: 12px 15px;
      
      .audit-form {
        .audit-buttons {
          gap: 15px;
          
          .el-button {
            flex: 1;
            min-width: auto;
            padding: 8px 15px;
          }
        }
      }
    }
  }
}
</style>